//
//  HJPerfectInfomationVC.m
//  project
//
//  Created by hu on 2019/3/21.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJPerfectInfomationVC.h"
#import "HJAlertOneClickeView.h"
#import "HJDataPikerView.h"
#import "HJUpAvatorVC.h"

@interface HJPerfectInfomationVC ()

/**
 昵称输入框
 */
@property (weak, nonatomic) IBOutlet UITextField *nameTF;

/**
 提交按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *meetBtn;

/**
 生日输入框
 */
@property (weak, nonatomic) IBOutlet UITextField *birthdayTF;

/**
 性别
 */
@property (nonatomic,assign) NSInteger isSex;

/**
 生日按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *birthdayBtn;

/**
 昵称View
 */
@property (weak, nonatomic) IBOutlet UIView *nameView;

/**
 stackView
 */
@property (weak, nonatomic) IBOutlet UIStackView *sexStackView;

/**
 性别 男
 */
@property (weak, nonatomic) IBOutlet UIButton *manBtn;

/**
 性别 女
 */
@property (weak, nonatomic) IBOutlet UIButton *womanBtn;

/**
 是否弹框
 */
@property (nonatomic,assign) BOOL isAlert;

@end

@implementation HJPerfectInfomationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"完善个人资料";
    [self layerBorder];
    [self configRacUI];
    self.isSex = 1;
}

// 切线 贝塞尔
-(void)layerBorder {
    self.view.backgroundColor = [UIColor colorWithHex:0xffffff];
    [self.nameView rounded:UIRectCornerAllCorners radius:5 borderColor:[UIColor blackColor] borderWidth:1];
    [self.birthdayBtn rounded:UIRectCornerAllCorners radius:5 borderColor:[UIColor blackColor] borderWidth:1];
    [self.sexStackView rounded:UIRectCornerAllCorners radius:5 borderColor:[UIColor blackColor] borderWidth:1];
    [self.manBtn rounded:(UIRectCornerTopLeft | UIRectCornerBottomLeft) radius:5];
    [self.womanBtn rounded:(UIRectCornerTopRight | UIRectCornerBottomRight) radius:5];
    [self.meetBtn rounded:UIRectCornerAllCorners radius:5];
}
// RAC 监听信号
-(void)configRacUI {
    @weakify(self)
    [[self.nameTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length >=8;
    }] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        self.nameTF.text = [x substringToIndex:8];
    }];
    
    // 选择生日
    [[self.birthdayBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.nameTF resignFirstResponder];
        HJDataPikerView * datePicker = [HJDataPikerView initDatePicker];
        datePicker.title = @"填写生日";
        datePicker.completeDatePickerBlock = ^(NSString * _Nonnull year, NSString * _Nonnull month, NSString * _Nonnull day, NSString * _Nonnull date) {
            @strongify(self)
            x.selected = !x.selected;
            self.birthdayBtn.selected = x.selected;
            self.birthdayTF.text = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
        };
        [datePicker show];
    }];
    // 下一步
    RAC(self.meetBtn, userInteractionEnabled) = [RACSignal combineLatest:@[(self.nameTF.rac_textSignal), RACObserve(self.birthdayBtn, selected),self.birthdayTF.rac_textSignal] reduce:^id ( NSString * name, NSNumber* birthdaySelecte) {
        @strongify(self)
        if (name.length >0 && [birthdaySelecte boolValue]) {
            self.meetBtn.backgroundColor = [UIColor blackColor];
            return @YES;
        }
        self.meetBtn.backgroundColor = [UIColor lightGrayColor];
        return @NO;
    }];
}


- (IBAction)girlOrManBtnCliclk:(UIButton *)sender {
    if (!self.isAlert) {
        HJAlertOneClickeView * alertView = [HJAlertOneClickeView alertOneView];
        alertView.actionBlock = ^{
            LKLog(@"点击好");
        };
        [alertView show];
        self.isAlert = YES;
    }
    [self animationWithBtn:sender];
}
-(void)animationWithBtn:(UIButton *)btn {
    for (int i=101; i <=102; i++) {
        UIButton * view = [self.view viewWithTag:i];
        view.selected = NO;
        view.backgroundColor = [UIColor clearColor];
    }
    btn.selected = YES;
    btn.backgroundColor = [UIColor blackColor];
    self.isSex = btn.tag-100;
}


- (IBAction)pushBtnClicked:(UIButton *)sender {
    NSMutableDictionary * parama = [NSMutableDictionary dictionary];
    parama[@"sex"] = @(self.isSex);
    parama[@"nickName"] = self.nameTF.text;
    parama[@"userId"] = [HJUserManager getUserModel].userId;
    parama[@"birthday"] = self.birthdayTF.text;
    HJUpAvatorVC * avatorVC =[HJUpAvatorVC new];
    avatorVC.dic = parama.mutableCopy;
    [self presentViewController:avatorVC animated:YES completion:nil];
}


//- (IBAction)backButtonClicked {
//    if (self.islogin) {
//        [self dismissViewControllerAnimated:YES completion:nil];
//    }else {
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}


@end
